<template>
  <section class="ling-top-text have_task_detail_wrap">
    <h2>任务详情</h2>
    <TaskDetail :jobDetailOrUserTaskDetail="jobDetail" />
    <div v-if="identity != 'company'" class="ling-flexBtn">
      <span
        class="ling-linearButton have_task_detail_btn"
        @click="handleGetOrder"
        >立即抢单</span
      >
    </div>
  </section>
</template>

<script>
import TaskDetail from '../components/TaskDetail'
import { getIdentity } from '@/utils/userAuth'
import { createNamespacedHelpers } from 'vuex'
const { mapState, mapActions } = createNamespacedHelpers('myData')

export default {
  name: 'jobDetailsPage',
  data() {
    return {
      identity: getIdentity(),
    }
  },
  async created() {
    await this.getJobDetail(this.$route.params.id)
    if (this.jobDetail.depositAmount) {
      await this.getUserPayAuth(window.location.href)
      await this.getWXConfig(window.location.href)
    }
  },
  components: {
    TaskDetail,
  },
  inject: ['reload'],
  computed: {
    ...mapState(['jobDetail', 'userDetail', 'payParams']),
  },
  methods: {
    ...mapActions([
      'getJobDetail',
      'createUserJob',
      'getUserPayAuth',
      'getWXConfig',
      'getPayParams',
    ]),
    handleGetOrder() {
      if (this.identity) {
        if (this.userDetail.dataPerfection) {
          this.$dialog
            .alert({
              message:
                '已通知需求方，请保持电话畅通稍后需求方会与您联系请到“我的—已接任务”中进行查看任务进度',
            })
            .then(() => {
              if (this.jobDetail.depositAmount) {
                wx.ready(async () => {
                  await this.getPayParams(this.$route.params.id)
                  if (!this.payParams) return
                  const params = this.payParams.pay_params
                  // const record_id = this.payParams.record_id
                  const _this = this
                  wx.chooseWXPay({
                    timestamp: params.timeStamp,
                    nonceStr: params.nonceStr,
                    package: params.packageStr,
                    signType: params.signType,
                    paySign: params.paySign,
                    success: async (res) => {
                      _this.createUserJob(this.jobDetail.id)
                      _this.$router.go(-1)
                    },
                  })
                })
              } else {
                this.createUserJob(this.jobDetail.id)
                this.$router.go(-1)
              }
            })
        } else {
          this.$router.push('/improvementPersonal')
        }
      } else {
        this.$router.push('/login')
      }
    },
  },
}
</script>

<style lang="less" scoped></style>
